package com.supor.aiot.basket.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.supor.aiot.basket.R;
import com.supor.aiot.basket.bean.IngredientListBean;
import com.supor.aiot.basket.ui.basket.IngredientListActivity;
import com.supor.aiot.basket.view.DoubleClickUtils;

import java.util.List;


public class IngreidenetListAdapter extends RecyclerView.Adapter<IngreidenetListAdapter.BasketHolder>{
    private List<IngredientListBean> mList;
    private Context mContext;
    private OnItemClickListener clickListener;
    private boolean showState;
    private String BATCH_NO = "BATCH_NO";
    private String BATCH_TIME = "BATCH_TIME";

    public IngreidenetListAdapter(List<IngredientListBean> mList, Context mContext) {
        this.mList = mList;
        this.mContext = mContext;
    }

    public void setOnItemClickListener(OnItemClickListener listener){
        this.clickListener = listener;
    }

    public void setIsAllSelected(boolean state){
        for (int i = 0;i<mList.size();i++){
            mList.get(i).setSelected(state);
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public IngreidenetListAdapter.BasketHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(mContext).inflate(R.layout.item_list_outer,parent, false);
        return new IngreidenetListAdapter.BasketHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull BasketHolder holder, int position) {
        IngredientListBean listBean = mList.get(position);
        holder.cbForAll.setVisibility(showState?View.VISIBLE:View.GONE);
        holder.cbForAll.setOnCheckedChangeListener((compoundButton, b) -> {
            clickListener.OnListSelected(position,b);
        });
        holder.cbForAll.setChecked(listBean.isSelected());

        holder.rlIngredientOuter.setOnClickListener(view -> {
            if (DoubleClickUtils.isFastDoubleClick())return;
            Intent intent = new Intent(mContext, IngredientListActivity.class);
            intent.putExtra(BATCH_NO,listBean.getListBatchno());
            intent.putExtra(BATCH_TIME,listBean.getLastUpdate());
            mContext.startActivity(intent);
        });

        if (!TextUtils.isEmpty(listBean.getListName()))holder.tvListTitle.setText(listBean.getListName());
        if (!TextUtils.isEmpty(listBean.getLastUpdate()))holder.tvListTime.setText(listBean.getLastUpdate());
    }

    public void changeCBstate(boolean state){
        this.showState=state;
    };


    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class BasketHolder extends RecyclerView.ViewHolder{
        private RelativeLayout rlIngredientOuter;
        private TextView tvListTitle;
        private TextView tvListTime;
        private CheckBox cbForAll;
        public BasketHolder(@NonNull View itemView) {
            super(itemView);
            rlIngredientOuter = itemView.findViewById(R.id.rl_ingredient_list);
            tvListTitle = itemView.findViewById(R.id.tv_ingredient_title);
            tvListTime = itemView.findViewById(R.id.tv_list_time);
            cbForAll = itemView.findViewById(R.id.cb_for_all);
        }
    }

    public interface OnItemClickListener{
        void OnListSelected(int position,boolean state);
    }

}
